<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Erra_Core_Config {

    public static $config = null;
    public static $applicationLoader = null;

    public static function check_ini() {
        $properties = array('available_langs', 'session_lifetime', 'db_driver', 'db_host',
            'db_name', 'db_user', 'log_messages', 'synchronize_schema', 'public_images',
            'public_style', 'public_js', 'public_upload_directory');
        // Check if mandatory properties are void
        foreach ($properties as $property) {
            if (!isset(self::$config[$property]) || is_null(self::$config[$property])) {
                Erra_Core_Kernel::fatalError('The property `' . $property . '` is missing from `application/' . INI_FILE . '`');
            }
        }

        // available langs
        $is_valid = true;
        $all_langs = array_keys(Erra_Core_Locale::getCodes());
        $langs = self::$config['application']['available_langs'];
        $langs = explode(',', $langs);
        foreach ($langs as $lang) {
            $lang = trim($lang);
            if (strlen($lang) && !in_array($lang, $all_langs)) {
                Erra_Core_Kernel::fatalError("The parameter `available_langs` in " . INI_FILE . " only accept international ISO codes");
            }
        }
    }

    public static function createApplicationSuperglobal() {
        global $_APPLICATION;
        $_APPLICATION = self::$config;
    }

    public static function get($property) {
        $strProperty = $property;
        $property = (strstr($property, '.') !== FALSE) ? explode('.', $property) : $property;
        $value = null;
        if (self::$config == null) {
            $ini_file = DOCUMENT_ROOT . '/application/' . INI_FILE;
            // Try to get domain-related ini file
            if (file_exists(DOCUMENT_ROOT . '/' . 'application/' . $_SERVER['HTTP_HOST'] . '.' . INI_FILE)) {
                $ini_file = DOCUMENT_ROOT . '/' . 'application/' . $_SERVER['HTTP_HOST'] . '.' . INI_FILE;
            }
            $values = parse_ini_file($ini_file);
            $associative_values = parse_ini_string(file_get_contents($ini_file), true);
            self::$config = array_merge($values, $associative_values);
            self::check_ini();
        }

        if (is_array($property) && count($property == 2)) {
            $section = $property[0];
            $item = $property[1];
            if (isset(self::$config[$section][$item])) {
                $value = self::$config[$section][$item];
                if (strstr($value, '{') !== FALSE && strstr($value, '}') !== FALSE) {
                    $part1 = str_replace('{', '', strstr($value, '}', true));
                    $part2 = str_replace('}', '', strstr($value, '}'));
                    $value = (self::get($part1) != null) ? self::get($part1) : '';
                    $value = $value . $part2;
                }
            }
        } else if (isset(self::$config[$property])) {
            $value = self::$config[$property];
            if (is_string($value)) {
                if (strstr($value, '{') !== FALSE && strstr($value, '}') !== FALSE) {
                    $part1 = str_replace('{', '', strstr($value, '}', true));
                    $part2 = str_replace('}', '', strstr($value, '}'));
                    $value = (self::get($part1) != null) ? self::get($part1) : '';
                    $value = $value . $part2;
                }
            }
        }
        if (is_null($value)) {
            if ($strProperty == 'http_host' || $strProperty == 'application.http_host') {
                if (strpos($_SERVER["HTTP_HOST"], $_SERVER["SERVER_NAME"]) !== false && !preg_match("/^[a-zA-Z0-9]*$/", $_SERVER["HTTP_HOST"])) {
                    $value = 'http://' . $_SERVER['HTTP_HOST'];
                }
            }
            if ($strProperty == 'webroot' || $strProperty == 'application.http_webroot') {
                if (strpos($_SERVER["HTTP_HOST"], $_SERVER["SERVER_NAME"]) !== false && !preg_match("/^[a-zA-Z0-9]*$/", $_SERVER["HTTP_HOST"])) {
                    $relative_part = str_replace(array($_SERVER['DOCUMENT_ROOT'], '/index.php'), '', $_SERVER['SCRIPT_FILENAME']);
                    $value = 'http://' . $_SERVER['HTTP_HOST'] . $relative_part;
                }
            }
        }

        if ($strProperty == 'http_host' || $strProperty == 'application.http_host' || $strProperty == 'webroot' || $strProperty == 'application.http_webroot') {
            $value = rtrim($value, '/');
        }
        return $value;
    }

}

?>
